<template>
  <div class="app-container">
    <SearchForm
      v-show="showSearch"
      v-model="queryParams"
      :options="options.formOps"
      @onQuery="getList"
    >
    </SearchForm>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['job:work:shop:post:add']"
          >新增</el-button
        >
      </el-col>

      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:user:remove']"
          >删除</el-button
        >
      </el-col> -->

      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:user:export']"
          >导出</el-button
        >
      </el-col> -->
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>
    <Table
      ref="table"
      :auto-featch="true"
      :fetch-data="fetchData"
      :options="options.table"
    >
      <template #actionButton="{ row }">
        <el-button
          type="text"
          icon="Edit"
          @click="handleEdit(row)"
          v-hasPermi="['job:work:shop:post:edit']"
          >修改</el-button
        >

        <el-button
          type="text"
          icon="Delete"
          style="color: red"
          @click="handleDel(row)"
          v-hasPermi="['job:work:shop:post:delete']"
          >删除</el-button
        >
      </template></Table
    >
    <DialogVform
      :formJson="formJson"
      ref="dialogVform"
      @onSubmit="onSubmit"
      :title="dialogType == 'add' ? '新增工段岗位' : '修改工段岗位'"
      :slots="[
        'monitor',
        'workShopDirector',
        'technologyPersonCharge',
        'hse',
        'equipmentPersonCharge',
        'workShopLong',
      ]"
    >
      <template v-for="(item, key) of slots" #[key]>
        <el-form-item :label="item" :prop="key" label-position="right">
          <el-input v-model="form[key]" readonly @click="onInnerOpen(key)">
          </el-input>
        </el-form-item>
      </template>

      <TableSelect
        ref="userTableSelect"
        :queryApi="listUser"
        :options="[]"
        :rowKey="'userId'"
        rowLabel="nickName"
        @onConfirm="onUserConfirm"
        rowLabelText="用户昵称"
        :maxNum="1"
      >
        <template #formSlot="{ handleQuery, filterForm }">
          <el-col :span="8">
            <el-form-item label="部门名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.deptName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="用户名称">
              <el-input
                size="small"
                @change="handleQuery"
                v-model="filterForm.userName"
                clearable
                style="width: 100%"
                @keyup.enter="handleQuery"
              />
            </el-form-item>
          </el-col>
        </template>
        <template #tableSlot>
          <el-table-column
            label="部门"
            align="center"
            key="deptName"
            prop="dept.deptName"
          />
          <el-table-column
            label="用户名称"
            align="center"
            key="userName"
            prop="userName"
          />
          <el-table-column
            label="用户昵称"
            align="center"
            key="nickName"
            prop="nickName"
          />
        </template>
      </TableSelect>
    </DialogVform>
  </div>
</template>

<script setup>
import Table from "@/components/Table/index.vue";
import SearchForm from "@/components/searchForm/index";
import DialogVform from "@/components/dialogVform/index";
import { apis as APIS } from "@/api/zygl/gdgwwh.js";
import formJson from "./components/options.json";
import { deptTreeSelect, listUser } from "@/api/system/user";
import TableSelect from "@/components/tableSelect/index";

import { useAsyncData, useFormJsonNew } from "@/views/hooks/useAsyncData.js";
const { proxy } = getCurrentInstance();
const ops = useFormJsonNew(formJson, {
  sort: [],
  sortable: [],
});
const options = reactive({
  table: ops,
  formOps: ops.filter((i) =>
    ["workShop", "score", "content", "izManual", "cardType"].includes(i.prop)
  ),
});
const slots = ref({
  monitor: "班长",
  workShopDirector: "车间主任",
  technologyPersonCharge: "车间技术负责人",
  hse: "HSE工程师",
  equipmentPersonCharge: "车间设备负责人",
  workShopLong: "工段长",
});
const showSearch = ref(true);

const dialogVform = ref();

const table = ref();
const userTableSelect = ref();
const userType = ref("");
const onInnerOpen = (type) => {
  userType.value = type;
  let values =
    (form.value[type + "Ids"] && form.value[type + "Ids"].split(",")) || [];
  let names = (form.value[type] && form.value[type].split(",")) || [];
  userTableSelect.value.open(
    values.map((item, index) => ({
      value: values[index],
      label: names[index],
    }))
  );
};
const onUserConfirm = (data) => {
  const values = data.map((item) => item.value);
  const names = data.map((item) => item.label);
  form.value[userType.value] = names.join(",");
  form.value[userType.value + "Ids"] = values.join(",");
};
// 补充修改.新增事件
const plugEdit = (row) => {
  form.value.hse = row.hse;
  form.value.monitor = row.monitor;
  form.value.workShopDirector = row.workShopDirector;
  form.value.technologyPersonCharge = row.technologyPersonCharge;
  form.value.equipmentPersonCharge = row.equipmentPersonCharge;
  form.value.workShopLong = row.workShopLong;
  form.value.hseIds = row.hseIds;
  form.value.monitorIds = row.monitorIds;
  form.value.workShopDirectorIds = row.workShopDirectorIds;
  form.value.technologyPersonChargeIds = row.technologyPersonChargeIds;
  form.value.equipmentPersonChargeIds = row.equipmentPersonChargeIds;
  form.value.workShopLongIds = row.workShopLongIds;
};
const {
  onSubmit,
  fetchData,
  handleAdd,
  handleEdit,
  getList,
  handleDel,
  queryParams,
  dialogType,
  form,
} = useAsyncData({ APIS, formJson, dialogVform, table, proxy, plugEdit });
</script>

<style lang="scss" scoped></style>
